﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using System.Data.Common;
using System.Collections;
using System.Collections.Specialized;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Privider for comunication with the database
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Saves the widget settings.
        /// </summary>
        /// <param name="widget">Associated widget</param>
        /// <param name="ws">The widget settings</param>
        public override void SaveWidgetSettings(Widget widget, StringDictionary ws)
        {
            try
            {
                connection.Open();
                foreach (DictionaryEntry setting in ws)
                {
					DbParameter widgetID = CreateParameter("widgetID", widget.ID);
                    DbParameter settingName = CreateParameter("settingName", setting.Key);
                    DbParameter settingValue = CreateParameter("settingValue", setting.Value);
                    ExecuteNonQuery("widgetSettings/SaveSettings", widgetID, settingName, settingValue);
                }
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Saves default widget settings.
        /// </summary>
        /// <param name="widget">Associated widget</param>
        /// <param name="ws">The widget settings</param>
        public override void SaveDefaultWidgetSettings(Widget widget, StringDictionary ws)
        {
            try
            {
                connection.Open();
                foreach (DictionaryEntry setting in ws)
                {
                    DbParameter widgetID = CreateParameter("widgetID", widget.ID);
                    DbParameter settingName = CreateParameter("settingName", setting.Key);
                    DbParameter settingValue = CreateParameter("settingValue", setting.Value);
                    ExecuteNonQuery("widgetSettings/SaveDefaultSettings", widgetID, settingName, settingValue);
                }
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Saves the widget settings.
        /// </summary>
        /// <param name="widget">Associated widget</param>
        /// <param name="key">Key value to save</param>
        /// <param name="value">The widget settings</param>
		public override void SaveWidgetSetting(Widget widget, string key, string value)
        {
            try
            {
                DbParameter widgetID = CreateParameter("widgetID", widget.ID);
                DbParameter settingName = CreateParameter("settingName", key);
                DbParameter settingValue = CreateParameter("settingValue", value);

                connection.Open();
                ExecuteNonQuery("widgetSettings/SaveSetting", widgetID, settingName, settingValue);
            }
            
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Loads the widget settings.
        /// </summary>
        /// <param name="widget">The widget.</param>
        /// <returns>Settings of the widget</returns>
        public override StringDictionary LoadWidgetSettings(Widget widget)
        {
            StringDictionary settings = new StringDictionary();
            try
            {
                DbParameter widgetID = CreateParameter("widgetID", widget.ID);

                connection.Open();
                DbDataReader data = GetVector("widgetSettings/LoadSettings", widgetID);

                while (data.Read())
                {
                    settings.Add(
                        GetStringByColumn(data, "NAME"),
                        GetStringByColumn(data, "VALUE"));
                }
            }
            finally
            {
                connection.Close();
            }
            return settings;
        }

        /// <summary>
        /// Update the setting.
        /// </summary>
        /// <param name="widget">The widget.</param>
        /// <param name="key">key value to delete</param>
        /// <returns>Specified widget settings.</returns>
		public override void UpdateWidgetSetting(Widget widget, string key, string value)
        {
            try
            {
                DbParameter widgetID = CreateParameter("widgetID", widget.ID);
                DbParameter settingName = CreateParameter("settingName", key);
                DbParameter settingValue = CreateParameter("settingValue", value);

                connection.Open();
                ExecuteNonQuery("widgetSettings/UpdateSetting", widgetID, settingName, settingValue);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Saves the widget settings.
        /// </summary>
        /// <param name="widget">Associated widget.</param>
        /// <param name="key">The widget settings.</param>
		public override void DeleteSetting(Widget widget, string key)
        {
            try
            {
                DbParameter widgetID = CreateParameter("widgetID", widget.ID);
                DbParameter settingName = CreateParameter("settingName", key);

                connection.Open();
                ExecuteNonQuery("widgetSettings/DeleteSettings", widgetID, settingName);
                
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
